package br.com.upis.sispat.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import br.com.upis.sispat.dao.Dao;
import br.com.upis.sispat.entity.Patrimonio;
import br.com.upis.sispat.entity.Requisicao;
import br.com.upis.sispat.exception.SeamCoreException;
import br.com.upis.sispat.service.PatrimonioService;
import br.com.upis.sispat.service.RequisicaoService;

@Service("requisicaoService")
public class RequisicaoServiceImpl implements RequisicaoService {

	private Dao<Requisicao> requisicaoDao;
	private PatrimonioService patrimonioService;

	private static final long serialVersionUID = -3306452448892268624L;

	@SuppressWarnings("unchecked")
	public List<Requisicao> search(Requisicao requisicao) throws SeamCoreException {

		StringBuilder hql = new StringBuilder();
		hql.append(" select u from Requisicao u ");
		hql.append(" where 1=1 ");


		if (requisicao.getDataReqisicao() != null) {
			hql.append(" and u.dataReqisicao = ? ");
		}
		
		if (requisicao.getJustificativa() != null
				&& requisicao.getJustificativa().length() > 0) {
			hql.append(" and u.justificativa like ? ");
		}
		
		if (requisicao.getStatusRequisicao() != null
				&& requisicao.getStatusRequisicao() > 0) {
			hql.append(" and u.statusRequisicao = ? ");
		}
		
		if (requisicao.getIdSetorPerfilNaoGestor() != null
				&& requisicao.getIdSetorPerfilNaoGestor() > 0) {
			hql.append(" and u.usuario.setor.codigo = ? ");
		}
		
		if (requisicao.getIdUsuarioPerfilNaoGestor() != null
				&& requisicao.getIdUsuarioPerfilNaoGestor() > 0) {
			hql.append(" and u.usuario.codigo = ? ");
		}
		
		if (requisicao.getPatrimonio() != null
				&& requisicao.getPatrimonio().getCodigo() != null && requisicao.getPatrimonio().getCodigo() > 0) {
			hql.append(" and u.patrimonio.codigo = ? ");
		}

		int i = 0;

		Session session = requisicaoDao.getSession();

		Transaction tx = session.getTransaction();

		tx.begin();

		Query query = session.createQuery(hql.toString());

		if (requisicao.getDataReqisicao() != null) {

			query.setDate(i++, requisicao.getDataReqisicao());
		}
		
		if (requisicao.getJustificativa() != null
				&& requisicao.getJustificativa().length() > 0) {

			query.setString(i++, "%" + requisicao.getJustificativa() + "%");
		}
		
		if (requisicao.getStatusRequisicao() != null
				&& requisicao.getStatusRequisicao() > 0) {

			query.setInteger(i++, requisicao.getStatusRequisicao());
		}
		
		if (requisicao.getIdSetorPerfilNaoGestor() != null
				&& requisicao.getIdSetorPerfilNaoGestor() > 0) {

			query.setInteger(i++, requisicao.getIdSetorPerfilNaoGestor());
		}
		
		if (requisicao.getIdUsuarioPerfilNaoGestor() != null
				&& requisicao.getIdUsuarioPerfilNaoGestor() > 0) {

			query.setInteger(i++, requisicao.getIdUsuarioPerfilNaoGestor());
		}
		
		if (requisicao.getPatrimonio() != null
				&& requisicao.getPatrimonio().getCodigo() != null && requisicao.getPatrimonio().getCodigo() > 0) {

			query.setInteger(i++, requisicao.getPatrimonio().getCodigo().intValue());
		}

		List<Requisicao> lista = query.list();

		tx.commit();

		return lista;
	}

	@Override
	@Transactional
	public void save(Requisicao requisicao) throws SeamCoreException {

		try {
			requisicaoDao.create(requisicao);
		} catch (SQLException e) {
			throw new SeamCoreException(e);
		}

	}

	@Override
	@Transactional
	public void delete(Requisicao requisicao) throws SeamCoreException {
		try {
			requisicaoDao.delete(requisicao);
		} catch (SQLException e) {
			throw new SeamCoreException(e);
		}

	}

	@Override
	@Transactional
	public void update(Requisicao requisicao) throws SeamCoreException {
		try {
			requisicaoDao.update(requisicao);
		} catch (SQLException e) {
			throw new SeamCoreException(e);
		}

	}

	public Requisicao getById(Long id) {

		return requisicaoDao.getObjectFromId(Requisicao.class, id);
	}

	@Autowired
	public void setRequisicaoDao(Dao<Requisicao> requisicaoDao) {
		this.requisicaoDao = requisicaoDao;
	}

	@Autowired
	public void setPatrimonioService(PatrimonioService patrimonioService) {
		this.patrimonioService = patrimonioService;
	}

}
